<?php

namespace app\model;

use think\facade\Db;

class WarehouseModel
{
    public static function  list($param): array
    {
        $userIds = getScopeView($param['uid']);
        if ($userIds != 0) {
            $where[] = ['uid', 'in', $userIds];
        }
        $where[] = [
            'partner_id', '=', $param['partner_id']
        ];
        $where[] = ['status', '<', 3];
        if (isset($param['name'])) {
            $where[] = ['name', 'like', '%' . $param['name'] . '%'];
        }
        if (isset($param['business_type_id']) && $param['business_type_id'] > 0) {
            $where[] = ['business_type_id', '=', $param['business_type_id']];
        }
        if ($param['is_export'] == 1) {
            $param['limit'] = Db::table('xl_warehouse')->where([$where])->count();
        }
        $warehouseType = Db::table('xl_warehouse')
            ->where([$where])->limit(($param['page'] - 1) * $param['limit'], $param['limit'])
            ->select()->toArray();
        $count = Db::table('xl_warehouse')->where([$where])->count();
        return ['list' => $warehouseType, 'count' => $count];
    }

    public static function line_data($param): array
    {
//        ToDo 暂时去掉,等仓库添加了数据权限 放开
//        $where[] = [
//            'uid', 'like', $param['source'].''
//        ];
        $where[] = ['status', '<', 3];
        $warehouseType = Db::table('xl_warehouse')
            ->where([$where])
            ->field('id,name,longitude,dimension')
            ->select()->toArray();
        return $warehouseType;
    }

    /**
     * 获取所有仓库类型
     * @return void
     */
    public static function GetAllWarehouseType(): array
    {
        $warehouseType = Db::table('xl_warehouse_type')->where("status", "=", 1)
            ->select()->toArray();
        $redisLink = linkRedis(11);
        $warehouseTypeData = $redisLink->get('warehouse_type');
        if ($warehouseTypeData) {
            return json_decode($warehouseTypeData, true);
        } else {
            $redisLink->set('warehouse_type', json_encode($warehouseType));
            return $warehouseType;
        }
    }

    /**
     * 获取所有业务类型
     * @return void
     */
    public static function GetAllBussineType(): array
    {
        $businessType = Db::table('xl_business_type')->where("status", "=", 1)
            ->select()->toArray();
        $redisLink = linkRedis(11);
        $businessTypeData = $redisLink->get('business_type');
        if ($businessTypeData) {
            return json_decode($businessTypeData, true);
        } else {
            $redisLink->set('business_type', json_encode($businessType));
            return $businessType;
        }
    }

    /**
     * 新增所有仓库
     * @return void
     */
    public static function add($data)
    {
        return Db::table('xl_warehouse')->insert($data);
    }

    /**
     * 新增所有仓库
     * @return int
     */
    public static function edit($data, $id)
    {
        return Db::table('xl_warehouse')->where('id', '=', $id)->update($data);
    }

    /**
     * 删除仓库
     * @return void
     */
    public static function del($id)
    {
        return Db::table('xl_warehouse')->where('id', '=', $id)->update(['status' => 3, 'update_time' => getNowDateTime()]);
    }

    public static function info($id, $isType = 0)
    {
        $info = Db::table('xl_warehouse')->where('id', '=', $id)->field('id,name,longitude,dimension,enterprise_name,person_in_charge,mobile')->find();
        if ($isType == 1) {
            $info['type'] = 1;
        } elseif ($isType == 2) {
            $info['type'] = 2;
        }
        return $info;
    }
}
